<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="入库单号" prop="warehousingNo">
        <el-input
          v-model="queryParams.warehousingNo"
          placeholder="请输入入库单号"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 260px;"
        />
      </el-form-item>
      <el-form-item label="生产工单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入生产工单号"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 260px;"
        />
      </el-form-item>
      <el-form-item label="入库时间" prop="warehousingTime">
        <el-date-picker clearable
                        v-model="queryParams.warehousingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        style="width: 260px;"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <br/>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库" clearable style="width: 260px;">
          <el-option v-for="item in StoreCenterInfo" :key="item.id" :label="item.name" :value="item.id" >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ProductionWarehousing:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ProductionWarehousing:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductionWarehousingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="入库单号" align="center" prop="warehousingNo" width="180"/>
      <el-table-column label="生产工单ID" align="center" prop="orderId" width="180"/>
      <el-table-column label="生产工单号" align="center" prop="orderNo" width="180"/>
      <el-table-column label="仓库" align="center" prop="cname" />
      <el-table-column label="入库时间" align="center" prop="warehousingTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warehousingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span> {{ scope.row.status == 1 ? '已完成' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="getShow(scope.row)"
            v-hasPermi="['system:ProductionWarehousing:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看按钮 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <div style="border: 1px gainsboro solid;padding: 10px 10px 10px 30px">
        <div style="display: inline-block;width: 300px;"><p>入库单号：{{ this.ProductionWarehousingShow.warehousingNo }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>生产工单ID：{{ this.ProductionWarehousingShow.orderId }}</p></div>
        <div style="display: inline-block;width: 300px;"><p>生产工单号：{{ this.ProductionWarehousingShow.orderNo }}</p></div>
        <br>
        <div style="display: inline-block;width: 300px;"><p>仓库：{{ this.ProductionWarehousingShow.cname}}</p></div>
        <div style="display: inline-block;width: 300px;"><p>状态：{{ this.ProductionWarehousingShow.status == 1 ? '已完成':''}}</p></div>
        <div style="display: inline-block;width: 300px;"><p>入库时间：{{ this.ProductionWarehousingShow.warehousingTime }}</p></div>
      </div>
      <div style="border: 1px gainsboro solid;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-table :data="this.ProductionWarehousingShow.productionWarehousingItemList" :row-class-name="rowTblPurchaseOrderDetailIndex" @selection-change="handleTblPurchaseOrderDetailSelectionChange" ref="tblPurchaseOrderDetail">
            <el-table-column label="入库单ID" prop="warehousingId" width="200"/>
            <el-table-column label="产品ID" prop="productId" width="200"/>
            <el-table-column label="产品编码" prop="productCode" width="200"/>
            <el-table-column label="产品名称" prop="productName" width="200"/>
            <el-table-column label="入库数量" prop="quantity" width="200"/>
            <el-table-column label="备注" prop="remark" width="300"/>
          </el-table>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductionWarehousing, getProductionWarehousing, delProductionWarehousing, addProductionWarehousing, updateProductionWarehousing } from "@/api/system/ProductionWarehousing";
import {listStoreCenterInfo} from '@/api/system/StoreCenterInfo'

export default {
  name: "ProductionWarehousing",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProductionWarehousingItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品入库表格数据
      ProductionWarehousingList: [],
      // 产品入库明细表格数据
      productionWarehousingItemList: [],
      //仓库
      StoreCenterInfo: [],
      //详情
      ProductionWarehousingShow:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehousingNo: null,
        orderNo: null,
        warehouseId: null,
        warehousingTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
    this.getStoreCenterInfo();
  },
  methods: {
    /** 查询产品入库列表 */
    getList() {
      this.loading = true;
      listProductionWarehousing(this.queryParams).then(response => {
        this.ProductionWarehousingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //仓库
    getStoreCenterInfo() {
      this.loading = true;
      listStoreCenterInfo({categoryType: 1}).then(response => {
        this.StoreCenterInfo = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehousingNo: null,
        orderId: null,
        orderNo: null,
        warehouseId: null,
        warehousingTime: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.productionWarehousingItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加产品入库";
      this.reset();
      this.$router.push({
        path:'/ProductionWarehousing/add',
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductionWarehousing(id).then(response => {
        this.form = response.data;
        this.productionWarehousingItemList = response.data.productionWarehousingItemList;
        this.open = true;
        this.title = "修改产品入库";
      });
    },
    /** 查询详情*/
    getShow(row) {
      this.loading = true;
      const id = row.id || this.ids
      getProductionWarehousing(id).then(response => {
        this.ProductionWarehousingShow = response.data;
        this.open=true;
        this.loading = false;
        this.title = "查看";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productionWarehousingItemList = this.productionWarehousingItemList;
          if (this.form.id != null) {
            updateProductionWarehousing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductionWarehousing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品入库编号为"' + ids + '"的数据项？').then(function() {
        return delProductionWarehousing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 产品入库明细序号 */
    rowProductionWarehousingItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 产品入库明细添加按钮操作 */
    handleAddProductionWarehousingItem() {
      let obj = {};
      obj.productId = "";
      obj.productCode = "";
      obj.productName = "";
      obj.quantity = "";
      obj.remark = "";
      this.productionWarehousingItemList.push(obj);
    },
    /** 产品入库明细删除按钮操作 */
    handleDeleteProductionWarehousingItem() {
      if (this.checkedProductionWarehousingItem.length == 0) {
        this.$modal.msgError("请先选择要删除的产品入库明细数据");
      } else {
        const productionWarehousingItemList = this.productionWarehousingItemList;
        const checkedProductionWarehousingItem = this.checkedProductionWarehousingItem;
        this.productionWarehousingItemList = productionWarehousingItemList.filter(function(item) {
          return checkedProductionWarehousingItem.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProductionWarehousingItemSelectionChange(selection) {
      this.checkedProductionWarehousingItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ProductionWarehousing/export', {
        ...this.queryParams
      }, `ProductionWarehousing_${new Date().getTime()}.xlsx`)
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 重新获取数据
      vm.getList();
    });
  }
};
</script>
